package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.bo.BattleItemExcel;
import com.tcm.entity.dto.BattleItemUpdateDto;
import com.tcm.entity.dto.param.BattleItemPageParam;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.BatchImportResult;
import com.tcm.service.BattleItemService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/9/10 14:29
 */
@RestController
@RequestMapping("/pc/battle")
public class BattleController {

    @Autowired
    private BattleItemService battleItemService;

    /**
     * 添加对战题目
     * @param dto     对战题目数据封装
     * @param request 请求对象
     * @return 添加结果
     */
    @RequestMapping(value = "/item", method = RequestMethod.POST)
    public R addBattleItem(@RequestBody @Valid BattleItemUpdateDto dto, HttpServletRequest request) {
        return battleItemService.addBattleItem(dto, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 修改对战题目
     * @param dto     对战题目数据封装
     * @param request 请求对象
     * @return 添加结果
     */
    @RequestMapping(value = "/item", method = RequestMethod.PUT)
    public R modifyBattleItem(@RequestBody @Valid BattleItemUpdateDto dto, HttpServletRequest request) {
        if (Objects.isNull(dto.getId())) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return battleItemService.modifyBattleItem(dto, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 批量删除对战题目
     * @param ids 对战题目ID集合
     * @return 删除结果
     */
    @RequestMapping(value = "/item", method = RequestMethod.DELETE)
    public R removeBattleItem(@RequestParam("ids") List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return battleItemService.removeBattleItem(ids) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 分页获取对战题目列表
     * @param param 分页查询参数
     * @return 对战题目列表
     */
    @RequestMapping(value = "/item", method = RequestMethod.GET)
    public R getBattleItemListByPage(BattleItemPageParam param){
        return R.success(battleItemService.getBattleItemListByPage(param));
    }

    /**
     * 批量导入对战题目
     * @param file excel文件
     * @return 导入校验结果
     */
    @RequestMapping(value = "/item/import", method = RequestMethod.POST)
    public R batchImportBattleItem(@RequestParam MultipartFile file){
        BatchImportResult<BattleItemExcel> result = null;
        try {
            result = battleItemService.importBattleItem(file);
        } catch (IOException e) {
            return R.fail(0, e.getMessage());
        }
        return R.success(result);
    }

    /**
     * 确认导入对战题目
     * @param uuid 导入uuid
     * @param request 请求对象
     * @return 导入结果
     */
    @RequestMapping(value = "/item/import", method = RequestMethod.PUT)
    public R confirmBatchBattleItem(@RequestParam String uuid, HttpServletRequest request){
        return battleItemService.confirmBatchBattleItem(uuid, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.CACHE_NOT_EXIST);
    }

    /**
     * 导出校验失败对战题信息
     * @param uuid 导入uuid
     * @param response 响应对象
     * @return excel文件
     */
    @RequestMapping(value = "/item/import", method = RequestMethod.GET)
    public R exportImportFailedBattleItem(@RequestParam String uuid, HttpServletResponse response){
        try {
            battleItemService.exportImportFailedBattleItem(uuid, response);
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return null;
    }

    /**
     * 批量导出对战题目
     * @param param 查询条件
     * @param response 响应对象
     * @return 正常为null
     */
    @RequestMapping(value = "/item/export", method = RequestMethod.GET)
    public R exportBattleItem(BattleItemPageParam param, HttpServletResponse response){
        try {
            battleItemService.exportBattleItem(param, response);
        } catch (IOException e) {
            return R.fail(0, e.getMessage());
        }
        return null;
    }
}
